/**
 * Created by Administrator on 2017/9/28 0028.
 *   参数说明    请按照规定参数进行设置
 *    将图片按照2倍压缩 inputMulriple=2  inputWidth=0 inputHeight=0
 *    原图上传  inputMulriple=0
 *    压缩图片至400*600  inputMulriple=1 inputWidth=400 inputHeight=600
 */
/* 2015-09-28 上传图片*/
function convertImgToBase64(inputMulriple,inputWidth,inputHeight,url, callback, outputFormat){
  var canvas = document.createElement('CANVAS');
  var ctx = canvas.getContext('2d');
  var img = new Image;
  img.crossOrigin = 'Anonymous';
  img.onload = function(){
    var width = img.width;
    var height = img.height;
    // console.log(inputMulriple);
    // console.log(inputWidth);
    // console.log(inputHeight);
    var rate=0;
    canvas.width = width*1;
    canvas.height = height*1;
    if(inputHeight!=0 && inputWidth!=0){//指定宽高
      canvas.width = inputWidth;
      canvas.height = inputHeight;
      ctx.drawImage(img,0,0,width,height,0,0,width,height);
    }
    if(inputMulriple==0){//不压缩
      rate=1;
      canvas.width = width*rate;
      canvas.height = height*rate;
    }else{
      // 按比例压缩 倍数为  inputMulriple
      rate = (width<height ? width/height : height/width)/parseInt(inputMulriple);
      canvas.width = width*rate;
      canvas.height = height*rate;
    }
    ctx.drawImage(img,0,0,width,height,0,0,width*rate,height*rate);
    var dataURL = canvas.toDataURL(outputFormat || 'image/png');
    callback.call(this, dataURL);
    canvas = null;

  };
  img.src = url;
}
function getObjectURL(file) {
  var url = null ;
  if (window.createObjectURL!=undefined) {  // basic
    url = window.createObjectURL(file) ;
  } else if (window.URL!=undefined) {       // mozilla(firefox)
    url = window.URL.createObjectURL(file) ;
  } else if (window.webkitURL!=undefined) { // web_kit or chrome
    url = window.webkitURL.createObjectURL(file) ;
  }
  return url ;
}
/**
 * Created by Administrator on 2017/9/28 0028.
 *   参数说明    请按照规定参数进行设置
 *    将图片按照2倍压缩 inputMulriple=2  inputWidth=0 inputHeight=0
 *    原图上传  inputMulriple=0
 *    压缩图片至400*600  inputMulriple=1 inputWidth=400 inputHeight=600
 */

// 在input框 里添加upload函数   ID（testfile1）修改下
function upd(inputMulriple,inputWidth,inputHeight){
  var imageUrl = getObjectURL(document.getElementById("testfile1").files[0]);
  convertImgToBase64(inputMulriple,inputWidth,inputHeight,imageUrl, function(base64Img){
    // base64Img为转好的base64,放在img src直接前台展示(<img src="...." />)
    //alert(base64Img);
    // base64转图片不需要base64的前缀data:image/jpg;base64

    //alert(base64Img.split(",")[1]);

    $.ajax({
      url:'http://127.0.0.1/file/qiNiu',
      type:'POST', //GET
      async:true,    //或false,是否异步
      data:{
        imgs:base64Img.split(",")[1],
      },
      timeout:50000,    //超时时间
      dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
/*      beforeSend:function(xhr){
        console.log(xhr)
        console.log('发送前')
      },*/
      success:function(data,textStatus,jqXHR){
        console.log(data);
        console.log(textStatus);
        console.log(jqXHR)
      },
      error:function(xhr,textStatus){
        console.log('错误');
        console.log(xhr);
        console.log(textStatus)
      },
/*      complete:function(){
        console.log('结束')
      }*/
    });

    //console.log(base64Img);
  });
  event.preventDefault();
}

function uplo(inputMulriple,inputWidth,inputHeight){
    var imageUrl = getObjectURL(document.getElementById("imgs").files[0]);
    convertImgToBase64(inputMulriple,inputWidth,inputHeight,imageUrl, function(base64Img){
        // base64Img为转好的base64,放在img src直接前台展示(<img src="...." />)
        //alert(base64Img);
        // base64转图片不需要base64的前缀data:image/jpg;base64

        //alert(base64Img.split(",")[1]);

        $.ajax({
            url:'/upload/uploadImg.shtml',
            type:'POST', //GET
            async:true,    //或false,是否异步
            data:{
                images:base64Img.split(",")[1],
            },
            timeout:50000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            /*      beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                  },*/
            success:function(data,textStatus,jqXHR){
                console.log(data);
                console.log(textStatus);
                console.log(jqXHR)
            },
            error:function(xhr,textStatus){
                console.log('错误');
                console.log(xhr);
                console.log(textStatus)
            },
            /*      complete:function(){
                    console.log('结束')
                  }*/
        });

        //console.log(base64Img);
    });
    event.preventDefault();
}
/* 2015-09-28 */
